/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Client;

/**
 *
 * @author spok
 */

import java.util.*;
import java.awt.*;
import java.lang.*;
import java.io.*;
import java.net.*;
import plar.UserInput;

public class Client {



    public String server;
    public String user;
    public String password;
    public HashMap <String, Object> resources;
    public URLConnection browser;
    
    public Client(String s,String u,String p)
    {
        server=s;
        user =u;
        password = p;
        resources = new HashMap();
        
         try {
               File dir = new File("cache");
               if (!dir.exists())
                   dir.mkdir();
           }
          catch (Exception ex) {   
          }  
         resources.put("login",login());
    }
    
    public HashMap login()
    {
           return hashMapFromURL(server+"?action=login&user="+user+"&password="+password);
    }

    public HashMap getResources()
    {
       HashMap <String, String> r =  hashMapFromURL(server+"?action=resources");
       
       for ( String key :  r.keySet() )
           
       {
              resources.put(key,getResource(key));
       }
       return r;
    }

    public HashMap input(UserInput i)
    {
         return hashMapFromURL(server+"?action=input&keys="+i.keys.toString()+"&mouse="+i.mouse.toString());
    }
    
    public HashMap output()
    {
         return hashMapFromURL(server+"?action=output");
    }
    
    public Resource getResource(String name) 
    {
         Resource r = new Resource(name);
        try {

            URL u = new URL(server + "?action=get&resource=" + name);
               r.image = Toolkit.getDefaultToolkit().getImage(u);
                  } catch (MalformedURLException ex) {
            
        }
            /*
            URLConnection c = u.openConnection();
            InputStream i = c.getInputStream();
            int car;
            Vector <Byte> v=new Vector <Byte>();
            while((car=i.read())==0)
            {
            v.add(Byte.valueOf((byte)car));
            }
            Byte [] res;
            v.copyInto(res);
            Picture p = new Picture();
             */
           
         
            return r;
  
        
    }
    
    
    
    public HashMap hashMapFromURL(String strurl)
    {
         HashMap r = new HashMap();
       try
       {
            URL u = new URL(strurl);
            URLConnection c = u.openConnection();
            DataInputStream dis = new DataInputStream(c.getInputStream());
            String inputLine;
            while ((inputLine = dis.readLine()) != null) {
                r.put(getKey(inputLine),getValue(inputLine));
            }
       }catch(Exception ex)
       {
        
       }
        
      return r;
    }
  
    public String getKey(String s)
    { 
       String [] a=  s.split("=");
       return a[0];
    }
    
    public String getValue(String s)
    { 
       String [] a=s.split("=");
       return a[1];
    }

}
